package com.xxx.video.commen.dbs.utils;


import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.xxx.video.VideoApplication;
import com.xxx.video.commen.dbs.beans.DownloadHistoryBean;
import com.xxx.video.commen.dbs.beans.SearchFriendHistoryBean;
import com.xxx.video.commen.dbs.beans.SearchHistoryBean;
import com.xxx.video.commen.dbs.beans.UploadFileBean;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.common.dbs.greendao.DaoMaster;
import com.xxx.video.common.dbs.greendao.DaoSession;
import com.xxx.video.common.dbs.greendao.DownloadHistoryBeanDao;
import com.xxx.video.common.dbs.greendao.SearchFriendHistoryBeanDao;
import com.xxx.video.common.dbs.greendao.SearchHistoryBeanDao;
import com.xxx.video.common.dbs.greendao.UploadFileBeanDao;

import java.util.List;

public class DbUtils {

    private static DbUtils mDbUtils = null;
    /**
     * sql name
     */
    private static final String DB_NAME = "video_db";
    private DaoSession mDaoSession;

    private DbUtils() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(VideoApplication.getInstance(), DB_NAME);
        SQLiteDatabase mSQLiteDatabase = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(mSQLiteDatabase);
        mDaoSession = daoMaster.newSession();
    }

    public static DbUtils getInstance() {
        if (mDbUtils == null) {
            synchronized (DbUtils.class) {
                if (mDbUtils == null) {
                    mDbUtils = new DbUtils();
                }
            }
        }
        return mDbUtils;
    }

    /**
     * get download history bean
     *
     * @param offset offset
     * @param limit  limit
     * @return bean
     */
    public List<DownloadHistoryBean> getDownloadBeanList(int offset, int limit) {
        DownloadHistoryBeanDao downloadHistoryBeanDao = mDaoSession.getDownloadHistoryBeanDao();
        return downloadHistoryBeanDao.queryBuilder().orderAsc(DownloadHistoryBeanDao.Properties.CreateTime).offset(offset).limit(limit).build().list();
    }

    public DownloadHistoryBean getDownloadHistoryBean(String id) {
        DownloadHistoryBeanDao downloadHistoryBeanDao = mDaoSession.getDownloadHistoryBeanDao();

        return downloadHistoryBeanDao.load(id);
    }

    /**
     * get download history bean
     *
     * @return bean
     */
    public List<DownloadHistoryBean> getDownloadBeanList() {
        DownloadHistoryBeanDao downloadHistoryBeanDao = mDaoSession.getDownloadHistoryBeanDao();
        return downloadHistoryBeanDao.queryBuilder().orderDesc(DownloadHistoryBeanDao.Properties.CreateTime).build().list();
    }

    /**
     * add download history bean
     *
     * @param downloadHistoryBean bean
     */
    public void addDownloadHistoryBean(DownloadHistoryBean downloadHistoryBean) {
        try {
            DownloadHistoryBeanDao downloadHistoryBeanDao = mDaoSession.getDownloadHistoryBeanDao();
            downloadHistoryBeanDao.insertOrReplaceInTx(downloadHistoryBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * add download history bean
     */
    public void addDownloadHistoryBean(String videoId, String downloadUrl, String title, String cover, String fCreateTime, int fPlayTimes, int fCommentTimes) {
        DownloadHistoryBean downloadHistoryBean = new DownloadHistoryBean.Builder()
                .fId(videoId)
                .createTime(System.currentTimeMillis())
                .fTitle(title)
                .fPicurl(cover)
                .fVideo(downloadUrl)
                .fCreateTime(fCreateTime)
                .fPlayTimes(fPlayTimes)
                .fCommentTimes(fCommentTimes)
                .build();
        DbUtils.getInstance().addDownloadHistoryBean(downloadHistoryBean);
    }


    /**
     * clear all search history list bean
     */
    public void clearAllSearchHistoryListBean() {
        SearchHistoryBeanDao searchHistoryBeanDao = mDaoSession.getSearchHistoryBeanDao();
        searchHistoryBeanDao.deleteAll();
    }

    /**
     * get search history list
     *
     * @return list
     */
    public List<SearchHistoryBean> getSearchHistoryList() {
        SearchHistoryBeanDao searchHistoryBeanDao = mDaoSession.getSearchHistoryBeanDao();
        return searchHistoryBeanDao.queryBuilder().orderDesc(SearchHistoryBeanDao.Properties.CreateTime).limit(20).build().list();
    }

    /**
     * add search history bean
     *
     * @param searchHistoryBean bean
     */
    public void addSearchHistoryBean(SearchHistoryBean searchHistoryBean) {
        try {
            SearchHistoryBeanDao searchHistoryBeanDao = mDaoSession.getSearchHistoryBeanDao();
            searchHistoryBeanDao.insertOrReplaceInTx(searchHistoryBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * clear all search friend history list bean
     */
    public void clearAllSearchFriendHistoryListBean() {
        SearchFriendHistoryBeanDao searchFriendHistoryBeanDao = mDaoSession.getSearchFriendHistoryBeanDao();
        searchFriendHistoryBeanDao.deleteAll();
    }

    /**
     * get search friend  history list
     *
     * @return list
     */
    public List<SearchFriendHistoryBean> getFriendSearchHistoryList() {
        SearchFriendHistoryBeanDao searchFriendHistoryBeanDao = mDaoSession.getSearchFriendHistoryBeanDao();
        return searchFriendHistoryBeanDao.queryBuilder().orderDesc(SearchFriendHistoryBeanDao.Properties.CreateTime).limit(20).build().list();
    }

    /**
     * add search friend history bean
     *
     * @param searchFriendHistoryBean bean
     */
    public void addFriendSearchHistoryBean(SearchFriendHistoryBean searchFriendHistoryBean) {
        try {
            SearchFriendHistoryBeanDao searchFriendHistoryBeanDao = mDaoSession.getSearchFriendHistoryBeanDao();
            searchFriendHistoryBeanDao.insertOrReplaceInTx(searchFriendHistoryBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public UploadFileBean queryUploadFileByPath(String path) {
        if (TextUtils.isEmpty(path)) {
            return null;
        }
        UploadFileBeanDao uploadFileBeanDao = mDaoSession.getUploadFileBeanDao();
        List<UploadFileBean> uploadFileBeans = uploadFileBeanDao.queryBuilder().where(UploadFileBeanDao.Properties.Path.eq(path)).build().list();
        if (ListUtils.isEmpty(uploadFileBeans)) {
            return null;
        }
        return uploadFileBeans.get(0);
    }

    public void insertUploadFileByPath(String path) {
        try {
            UploadFileBeanDao uploadFileBeanDao = mDaoSession.getUploadFileBeanDao();
            UploadFileBean uploadFileBean = new UploadFileBean();
            uploadFileBean.path = path;
            uploadFileBean.isUpload = true;
            uploadFileBeanDao.insertOrReplace(uploadFileBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
